//
//  PzCoinCtrl.swift
//  Pz
//
//  Created by 蓝鳍互娱 on 2024/6/26.
//

import Foundation
import UIKit
class PzVipCtrl: UIViewController {
    var applePayScuessBlock: (() -> Void)?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = true
        view.backgroundColor = .clear
        creatSubView()
    }
    lazy var vipBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzVipBg")
        return imageView
    }()
    lazy var vipNavBackBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(vipNavBack), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzVipClose"), for: .normal)
        return btn
    }()
    lazy var vipPayBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(vipPay), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzVipBuyBg"), for: .normal)
        btn.setTitle("继续试用", for: .normal)
        btn.setTitleColor(UIColor(hex: 0xFFFFFF), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 32.fitScale(), weight: .regular)
        return btn
    }()
    lazy var vipUserAgreementBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("服务条款", for: .normal)
        btn.setTitleColor(UIColor(hex: 0x8D40FF), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 24.fitScale(), weight: .regular)
        btn.addTarget(self, action: #selector(vipUserAgreement), for: .touchUpInside)
        return btn
    }()
    lazy var vipProviceAgreementBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("隐私协议", for: .normal)
        btn.setTitleColor(UIColor(hex: 0x8D40FF), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 24.fitScale(), weight: .regular)
        btn.addTarget(self, action: #selector(vipProviceAgreement), for: .touchUpInside)
        return btn
    }()
    lazy var vipGoPayBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("恢复购买", for: .normal)
        btn.setTitleColor(UIColor(hex: 0x8D40FF), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 24.fitScale(), weight: .regular)
        btn.addTarget(self, action: #selector(vipGoPay), for: .touchUpInside)
        return btn
    }()
    lazy var vipPriceLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 34.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0xFFFFFF)
        lab.numberOfLines = 0
        return lab
    }()
    func creatSubView(){
        self.view.addSubview(vipBgImgView)
        self.view.addSubview(vipNavBackBtn)
        self.view.addSubview(vipPriceLab)
        self.view.addSubview(vipPayBtn)
        self.view.addSubview(vipUserAgreementBtn)
        self.view.addSubview(vipProviceAgreementBtn)
        self.view.addSubview(vipGoPayBtn)
        
        vipBgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        vipNavBackBtn.snp.makeConstraints { make in
            make.right.equalTo(-24.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 12.fitScale())
            make.height.height.equalTo(64.fitScale())
            make.width.height.equalTo(64.fitScale())
        }
        vipPriceLab.snp.makeConstraints { make in
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 867.fitScale())
            make.left.equalTo(60.fitScale())
            make.right.equalTo(-60.fitScale())
        }
        vipPayBtn.snp.makeConstraints { make in
            make.left.equalTo(76.fitScale())
            make.right.equalTo(-76.fitScale())
            make.bottom.equalTo(-155.fitScale())
            make.height.equalTo(86.fitScale())
        }
        vipProviceAgreementBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(vipPayBtn.snp.bottom).offset(32.fitScale())
            make.height.equalTo(40.fitScale())
        }
        vipUserAgreementBtn.snp.makeConstraints { make in
            make.right.equalTo(vipProviceAgreementBtn.snp.left).offset(-30.fitScale())
            make.top.equalTo(vipProviceAgreementBtn)
            make.height.equalTo(40.fitScale())
        }
        vipGoPayBtn.snp.makeConstraints { make in
            make.left.equalTo(vipProviceAgreementBtn.snp.right).offset(30.fitScale())
            make.top.equalTo(vipProviceAgreementBtn)
            make.height.equalTo(40.fitScale())
        }
        if PzPublicData.shareManage.price != nil{
            let attString = String(format: "解锁全部高级功能，可无限次使用“证件照拍摄”、“服装变换”、“妆容精修”等功能，免费试用三天，可自动续订，续费￥%1@/年，免费享受后续功能迭代，无需额外付费，随时取消", PzPublicData.shareManage.price!)
            let changeString = String(format: "续费￥%1@/年", PzPublicData.shareManage.price!)

            if let changeRange = attString.range(of: changeString) {
                let attMutStr = NSMutableAttributedString(string: attString)
                attMutStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: attString.count))
                attMutStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 34.fitScale(), weight: .regular), range: NSRange(location: 0, length: attString.count))
                attMutStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 40.fitScale(), weight: .bold), range: NSRange(changeRange,in: attString))

                vipPriceLab.attributedText = attMutStr
            }
        }
    }
    func saveBuyVipTime(){
        let currentDate = Date()
        let calendar = Calendar.current
        var dateComponents = DateComponents()
        dateComponents.year = 1
        if let newDate = calendar.date(byAdding: dateComponents, to: currentDate) {
            let currentTimeStamp = newDate.timeIntervalSince1970
            let timestamp = Double(currentTimeStamp) // 如果需要毫秒精度
            UserDefaults.standard.setValue(timestamp, forKey: "pzDefTime")
            UserDefaults.standard.synchronize()
        }
    }
    @objc func vipPay(){
        PzApplePayHelper.shareManage.startApplePay(productId: "kankannianhuiyuan")
        PzApplePayHelper.shareManage.applePayScuessBlock = {
            self.saveBuyVipTime()
            self.applePayScuessBlock?()
            self.dismiss(animated: true)
            PzPublicData.shareManage.requestVipMaiDian()
        }
    }
    @objc func vipUserAgreement(){
        let webCtrl = PzWebCtrl()
        webCtrl.webUrl = PzPublicConst.photoUserAgreementUrl
        self.navigationController?.pushViewController(webCtrl, animated: true)
    }
    @objc func vipProviceAgreement(){
        let webCtrl = PzWebCtrl()
        webCtrl.webUrl = PzPublicConst.photoPriveAgreementUrl
        self.navigationController?.pushViewController(webCtrl, animated: true)
    }
    @objc func vipGoPay(){
        PzApplePayHelper.shareManage.startApplePay(productId: "kankannianhuiyuan")
        PzApplePayHelper.shareManage.applePayScuessBlock = {
            self.saveBuyVipTime()
            self.applePayScuessBlock?()
            self.dismiss(animated: true)
        }
    }
    @objc func vipNavBack(){
        self.dismiss(animated: true) {
            
        }
    }
}
